package com.ccl.iot.dvr.utils;

import android.view.View;
import android.view.animation.RotateAnimation;

/**
 * Created by HD on 2016/12/7.
 */

public class RoatAnimUtil {

    private static RotateAnimation mAnimation;

    /**
     * 开始动画，无延迟时间
     *
     * @param view
     */
    public static boolean startAnimation(View view) {
        return startAnimation(view, 0);
    }


    /**
     * 开始动画
     *
     * @param view
     * @param delay 为动画延迟的时间，单位是毫秒
     */
    public static boolean startAnimation(View view, long delay) {
        if (view != null && view.getWidth() != 0 && view.getHeight() != 0) {
            if (mAnimation == null) {
                mAnimation = new RotateAnimation(0, 360,
                        view.getWidth() / 2, view.getHeight() / 2);
                mAnimation.setDuration(1000);
                mAnimation.setRepeatCount(-1);
                mAnimation.setStartOffset(delay);
                mAnimation.setFillAfter(true);
                view.startAnimation(mAnimation);
            } else {
                view.startAnimation(mAnimation);
            }
            return true;
        }
        return false;
    }

    /**
     * 停止动画
     *
     * @param view
     */
    public static boolean stopAnimation(View view) {
        view.clearAnimation();
        if (mAnimation != null) {
            mAnimation.cancel();
            mAnimation = null;
            return true;
        }
        return false;
    }

}
